package cn.iocoder.yudao.module.lowcode.controller.admin.table;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.lowcode.service.table.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 表管理控制器
 * 展示如何在表操作中集成 Magic API 自动生成功能
 *
 * @author 芋道源码
 */
@Tag(name = "低代码 - 表管理")
@RestController
@RequestMapping("/lowcode/table-management")
@Validated
@Slf4j
public class TableManagementController {

    @Resource
    private TableManagementService tableManagementService;

    @PostMapping("/create")
    @Operation(summary = "创建表")
    @PreAuthorize("@ss.hasPermission('lowcode:table:create')")
    public CommonResult<Boolean> createTable(@Valid @RequestBody CreateTableRequest request) {
        log.info("[createTable] 创建表请求: tableName={}, dataSourceId={}", 
                request.getTableName(), request.getDataSourceId());
        
        tableManagementService.createTable(request);
        return success(true);
    }

    @PostMapping("/import")
    @Operation(summary = "批量导入表")
    @PreAuthorize("@ss.hasPermission('lowcode:table:import')")
    public CommonResult<Boolean> importTables(@Valid @RequestBody ImportTablesRequest request) {
        log.info("[importTables] 批量导入表请求: tableCount={}, dataSourceId={}", 
                request.getTableNames().size(), request.getDataSourceId());
        
        tableManagementService.importTables(request);
        return success(true);
    }

    @DeleteMapping("/drop")
    @Operation(summary = "删除表")
    @PreAuthorize("@ss.hasPermission('lowcode:table:drop')")
    public CommonResult<Boolean> dropTable(@Valid @RequestBody DropTableRequest request) {
        log.info("[dropTable] 删除表请求: tableName={}, dataSourceId={}", 
                request.getTableName(), request.getDataSourceId());
        
        tableManagementService.dropTable(request);
        return success(true);
    }

    @PutMapping("/alter")
    @Operation(summary = "修改表结构")
    @PreAuthorize("@ss.hasPermission('lowcode:table:alter')")
    public CommonResult<Boolean> alterTable(@Valid @RequestBody AlterTableRequest request) {
        log.info("[alterTable] 修改表结构请求: tableName={}, dataSourceId={}, operationCount={}", 
                request.getTableName(), request.getDataSourceId(), 
                request.getOperations() != null ? request.getOperations().size() : 0);
        
        tableManagementService.alterTable(request);
        return success(true);
    }

    @PostMapping("/sync-datasource")
    @Operation(summary = "同步数据源中的所有表")
    @Parameter(name = "dataSourceId", description = "数据源ID", required = true)
    @PreAuthorize("@ss.hasPermission('lowcode:table:sync')")
    public CommonResult<Boolean> syncDataSourceTables(@RequestParam("dataSourceId") Long dataSourceId) {
        log.info("[syncDataSourceTables] 同步数据源表请求: dataSourceId={}", dataSourceId);
        
        tableManagementService.syncDataSourceTables(dataSourceId);
        return success(true);
    }
}